/* 
 * 爱吧登录注册表单验证
 * tony <tony@yonglang.co>
 */

// 表单验证
$('.kv').append('<p class="kv-error"><i class="icon-error"></i><span class="kv-error-txt"></span></p>');
//======================验证码切换======================
// 切换验证码
var verifyURL;
$(document).on('click', '.captcha-img', function (e) {
    if (!verifyURL)
        verifyURL = this.src;
    this.src = verifyURL + '&t=' + new Date().getTime();
}).on('click', '.icon-refresh', function (e) {
    e.preventDefault();
    var img = $(this).prevAll('img')[0];
    if (!verifyURL)
        verifyURL = img.src;
    img.src = verifyURL + '&t=' + new Date().getTime();
});

//====================具体的验证方法=====================
var loginForm = $('#J_loginForm'),
        regForm = $('#J_regForm'),
        loginValide = {email: false, password: false, verify: false},
regValide = {email: false, password: false, repassword: false};
$('#verify,#email,#password,#reg_email,#reg_password,#repassword').on('focus', function () {
    var val = $(this).val(),
            errorElementSpan = $(this).parents('.kv').children('.kv-error'),
            errorElement = $(this).parents('.kv').children('.kv-error').children('.kv-error-txt'),
            labelElement = $(this).siblings('label');
    labelElement.css('display', 'none');
});
//绑定事件
$('#verify').on('blur', function () {
    var val = $(this).val(),
            errorElementSpan = $(this).parents('.kv').children('.kv-error'),
            errorElement = $(this).parents('.kv').children('.kv-error').children('.kv-error-txt'),
            labelElement = $(this).siblings('label');
    if (val == '') {
        loginValide.verify = false;
        labelElement.css('display', 'block');
        errorElement.text('请填写验证码');
        errorElementSpan.css('display', 'block');
        return;
    }
    $.post(U('home/Public/isVerifyAvailable'), {verify: val}, function (data) {
        if (data === 'success') {
            loginValide.verify = true;
            //取消错误提示
            errorElementSpan.css('display', 'none');
        } else {
            loginValide.verify = false;
            errorElement.text("验证码错误");
            errorElementSpan.css('display', 'block');
        }
    });
});

$('#email').on('blur', function () {
    //不能为空
    var val = $(this).val(),
            errorElementSpan = $(this).parents('.kv').children('.kv-error'),
            errorElement = $(this).parents('.kv').children('.kv-error').children('.kv-error-txt'),
            labelElement = $(this).siblings('label');
    if (val == '') {
        loginValide.email = false;
        errorElement.text('请填写用户名');
        labelElement.css('display', 'block');
        errorElementSpan.css('display', 'block');
        return;
    } else {
        loginValide.email = true;
        errorElementSpan.css('display', 'none');
    }
});
$('#password').on('blur', function () {
    //不能为空
    var val = $(this).val(),
            errorElementSpan = $(this).parents('.kv').children('.kv-error'),
            errorElement = $(this).parents('.kv').children('.kv-error').children('.kv-error-txt'),
            labelElement = $(this).siblings('label');
    if (val == '') {
        loginValide.password = false;
        errorElement.text('请填写密码');
        labelElement.css('display', 'block');
        errorElementSpan.css('display', 'block');
        return;
    } else {
        loginValide.password = true;
        errorElementSpan.css('display', 'none');
    }
});

//注册验证--start--
$('#reg_email').on('blur', function () {
    var val = $(this).val(),
            errorElementSpan = $(this).parents('.kv').children('.kv-error'),
            errorElement = $(this).parents('.kv').children('.kv-error').children('.kv-error-txt'),
            labelElement = $(this).siblings('label');
    if (val == '') {
        regValide.email = false;
        labelElement.css('display', 'block');
        errorElement.text('请填写注册邮箱');
        errorElementSpan.css('display', 'block');
        return;
    }
    var regex = /^[a-z\d]+(\.[a-z\d]+)*@([\da-z](-[\da-z])?)+(\.{1,2}[a-z]+)+$/;
    if (!regex.test(val)) {
        regValide.email = false;
        errorElement.text('请填写正确的邮箱号');
        errorElementSpan.css('display', 'block');
        return;
    }
    $.post(U('home/Public/isEmailAvailable'), {email: val}, function (data) {
        if (data === 'success') {
            regValide.email = true;
            errorElementSpan.css('display', 'none');
        } else {
            regValide.email = false;
            errorElement.text(data);
            errorElementSpan.css('display', 'block');
        }
    });
});

$('#reg_password').on('blur', function () {
    var val = $(this).val(),
            min = $(this).attr('min'),
            msg,
            errorElementSpan = $(this).parents('.kv').children('.kv-error'),
            errorElement = $(this).parents('.kv').children('.kv-error').children('.kv-error-txt'),
            labelElement = $(this).siblings('label');

    if (val.length < min) {
        if (val == '') {
            labelElement.css('display', 'block');
        }
        regValide.password = false;
        errorElement.text('密码不能少于 ' + min + ' 位');
        errorElementSpan.css('display', 'block');
        return;
    } else {
        // 后台设置密码规则
        $.post(U('home/Public/isVerifyPwdRole'), {password: val}, function (data) {
            if (data == 1) {
                msg = '密码必须含有数字';
            } else if (data == 2) {
                msg = '密码必须含有大写字母';
            } else if (data == 3) {
                msg = '密码必须含有小写字母';
            } else if (data == 4) {
                msg = '密码必须含有' + Pass_Symbol;
            } else if (data == 'success') {
                msg = 'ok';
            }
            if (msg != 'ok') {
                regValide.password = false;
                errorElement.text(msg);
                errorElementSpan.css('display', 'block');
            } else {
                regValide.password = true;
                errorElementSpan.css('display', 'none');
            }
        });
    }
});

$('#repassword').on('blur', function () {
    var val = $(this).val(),
            regVal = $('#reg_password').val(),
            errorElementSpan = $(this).parents('.kv').children('.kv-error'),
            errorElement = $(this).parents('.kv').children('.kv-error').children('.kv-error-txt'),
            labelElement = $(this).siblings('label');
    if (regVal == '') {
        regValide.repassword = false;
        labelElement.css('display', 'block');
        errorElement.text('请先填写密码');
        errorElementSpan.css('display', 'block');
        return;
    } else {
        if (regVal == val) {
            regValide.repassword = true;
            errorElementSpan.css('display', 'none');
        } else {
            regValide.repassword = false;
            errorElement.text('确认密码与原密码不相同');
            errorElementSpan.css('display', 'block');
        }
    }
});

//=======================登录，注册表单切换========================
// tab
$('.hd > .a').click(function (e) {
    var $hd = $(this).parent('.hd');
    var $a = $hd.children('.a');
    var $b = $hd.siblings('.bd').children('.b');

    if (!$(this).hasClass('cur')) {
//        if ($(this).index() === 0) {
//            //清空验证，重新验证
//            cur = window.location.hash;
//        } else {
//            cur = window.location.hash;
//        }
        var i = $(this).index($a);
        $(this).addClass('cur').siblings('a').removeClass('cur');
        $b.removeClass('cur');
        $b.eq(i).addClass('cur');
    }

    e.preventDefault();
});


//========================用户协议=========================
// 用户协议
$('.toagreement').click(function (e) {
    e.preventDefault();
    var $layer = $('.layer');
    if ($layer.length) {
        $layer.show();
    } else {
        var t = $('.main').position().top;
        $('body').append($('#agreement-template').html());
        $layer = $('.layer').css('top', t);
        $('.container').height($('.wrap').height() - t);
        $('.agreement').scrollbar({
            'content': '.agreement-content'
        });
        $('.close', $layer).click(function () {
            $layer.hide();
        });
    }
});


//提交
var loginEmail = $('#email'),
        loginPassword = $('#password'),
        loginVerify = $('#verify'),
        regEmail = $('#reg_email'),
        regPassword = $('#reg_password'),
        regRepassword = $('#repassword'),
        agreement = $('#xieyi');

loginForm.submit(function () {
    if (loginValide.email === false) {
        loginEmail.focus();
        return false;
    } else if (loginValide.password === false) {
        loginPassword.focus();
        return false;
    } else if (loginValide.verify === false) {
        loginVerify.focus();
        return false;
    } else {
        return true;
    }
});
regForm.submit(function () {
    if (!agreement.prop('checked')) {
        agreement.parents('.kv').children('.kv-error').children('.kv-error-txt').text('同意用户协议后才能注册');
        agreement.parents('.kv').children('.kv-error').css('display', 'block');
        return false;
    }
    if (regValide.email === false) {
        regEmail.focus();
        return false;
    } else if (regValide.password === false) {
        regPassword.focus();
        return false;
    } else if (regValide.repassword === false) {
        regRepassword.focus();
        return false;
    } else {
        return true;
    }
});


